<?php include('includes/header.php'); ?>

<?php
//check to see if form was submitted
if(isset($_POST["submit"]) && $_POST["email"] != "" && $_POST["password"] != "" && $_POST["confirm"] != "") 
{
	//check to see if email available
	include('includes/db.php');
	$result = mysql_query("SELECT email FROM users");
	$emails = array();
	while($email = mysql_fetch_array($result))
  	{
	  array_push($emails, $email["email"]);	
	}
	if(in_array($_POST["email"], $emails))
	{
		//email is taken
		$response = "This email is taken";	
	} else {
		//check to see if password matches
		if($_POST["password"] == $_POST["confirm"])
		{
			//run the query to add new user
			mysql_query("INSERT INTO users (name, email, password)
VALUES ('".mysql_real_escape_string($_POST["name"])."', '".mysql_real_escape_string($_POST["email"])."','".mysql_real_escape_string(md5($_POST["password"]))."')");
			header("Location: login");
		} else {
			$response = "The password didn't match";
		}
	}
} else if(isset($_POST["submit"])) {
	$response = "You forgot something";	
}
?>


<title>Lore Rider Wireframe</title>
<?php include('includes/resources.php'); ?>
<?php include('includes/links.php'); ?>
<style>
.form-signin {
        max-width: 300px;
        padding: 19px 29px 29px;
        margin: 0 auto 20px;
        background-color: #fff;
        border: 1px solid #e5e5e5;
        -webkit-border-radius: 5px;
           -moz-border-radius: 5px;
                border-radius: 5px;
        -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
           -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                box-shadow: 0 1px 2px rgba(0,0,0,.05);
      }
      .form-signin .form-signin-heading,
      .form-signin .checkbox {
        margin-bottom: 10px;
      }
      .form-signin input[type="text"],
      .form-signin input[type="password"] {
        font-size: 16px;
        height: auto;
        margin-bottom: 15px;
        padding: 7px 9px;
      }

</style>

<?php $_SESSION["signup"] = "current"; unset($_SESSION["login"]); unset($_SESSION["home"]); include('includes/body.php'); ?>

        <form class="form-signin" method="post">
        <h2 class="form-signin-heading">Sign Up For Free</h2>
        
        <input type="text" class="input-block-level" name="name" placeholder="Full Name" <?php if($_POST["name"] != "") echo "value=\"$_POST[name]\"" ?>>
        <input type="text" class="input-block-level" name="email" placeholder="Email address" <?php if($_POST["email"] != "") echo "value=\"$_POST[email]\"" ?>>
        <input type="password" class="input-block-level" name="password" placeholder="Password">
        <input type="password" class="input-block-level" name="confirm" placeholder="Confirm Password">
        <button class="btn btn-large btn-primary" type="submit" name="submit">Sign Up</button>
        <?php if(isset($response)) echo "<h4 class='alert'>" . $response . "</h4>"; ?>
      </form>

<?php include('includes/footer.php'); ?>
